<template>
	<view class="wrap">
		<view class="content">
			<view class="title">欢迎注册图书商城</view>
			<view class="mianbox">
				<view>
					<view class="iconfont icon-wode"></view>
					<input
						type="text"
						v-model="params.name"
						placeholder="请输入用户名"
					/>
				</view>
				<view>
					<view class="iconfont icon-icon-test33"></view>
					<input
						type="email"
						v-model="params.email"
						placeholder="请输入邮箱号"
					/>
				</view>
				<view>
					<view class="iconfont icon-icon-test26"></view>
					<input
						type="password"
						v-model="params.password"
						placeholder="请输入密码"
					/>
				</view>
				<view>
					<view class="iconfont icon-icon-test26"></view>
					<input
						type="password"
						v-model="params.password_confirmation"
						placeholder="请输入确认密码"
					/>
				</view>
			</view>
			<button @tap="handleRegister" class="getCaptcha">注册</button>
			<view class="alternative">
				<view class="password">其他方式</view>
				<view class="issue" @tap="$u.route('pages/auth/login')">
					登录
				</view>
			</view>
		</view>
		<!-- <view class="buttom">
			<view class="loginType">
				<view class="wechat item">
					<view class="icon">
						<u-icon size="70" name="weixin-fill" color="rgb(83,194,64)"></u-icon>
					</view>
					微信
				</view>
				<view class="QQ item">
					<view class="icon">
						<u-icon size="70" name="qq-fill" color="rgb(17,183,233)"></u-icon>
					</view>
					QQ
				</view>
			</view>
			<view class="hint">登录代表同意
				<text class="link">图书商城用户协议、隐私政策，</text>
				并授权使用您的图书商城账号信息（如昵称、头像、收获地址）
			</view>
		</view> -->
	</view>
</template>

<script>
export default {
	name: 'Register',
	data() {
		return {
			hidePwd: false,
			// 用户注册数据
			params: {
				name: '',
				email: '',
				password: '',
				password_confirmation: ''
			},
			rules: {
				// usertel:{
				// rule:  /^[1](([3][0-9])|([4][0,1,4-9])|([5][0-3,5-9])|([6][2,5,6,7])|([7][0-8])|([8][0-9])|([9][0-3,5-9]))[0-9]{8}$/,
				// 	msg:"手机号码输入不规范"
				// },
				name: {
					rule: /^[a-zA-Z0-9_-]{4,16}$/,
					msg: '4到16位（字母，数字，下划线，减号)'
				},
				email: {
					rule: /^\w+@[a-z0-9]+\.[a-z]+$/i,
					msg: '邮箱格式不对'
				},
				password: {
					rule: /^[0-9a-zA-Z]{6,16}$/,
					msg: '密码应该为6-16位'
				}
			}
			// 是否激活注册按钮
			// isRegister: false
		};
	},
	methods: {
		// 用户注册
		async handleRegister() {
			if (!this.validate('password')) return;
			if (!this.validate('name')) return;
			if (!this.validate('email')) return;
			if (
				this.params.password !== this.params.password_confirmation
			) {
				//提示信息
				uni.showToast({
					title: '密码不一致',
					icon: 'none'
				});
				return;
			}
			// 验证通过，请求注册
			const res = await this.$u.api.authUserRegister(this.params);
			this.$u.toast('注册成功');
			// 注册成功重定向到登录页
			setTimeout(() => {
				this.$u.route({
					type: 'redirect',
					url: 'pages/auth/login'
				});
			}, 1000);
		},
		//判断验证是否符合要求
		validate(key) {
			console.log(key);
			let bool = true;
			if (!this.rules[key].rule.test(this.params[key])) {
				//提示信息
				uni.showToast({
					title: this.rules[key].msg,
					icon: 'none'
				});
				//取反
				bool = false;
				return false;
			}
			return bool;
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap {
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	height: 100vh;
	.content {
		width: 600rpx;
		.title {
			text-align: center;
			font-size: 60rpx;
			font-weight: 500;
			margin-bottom: 50rpx;
		}
	}
	.mianbox > view {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		height: 100rpx;
		border-bottom: 1rpx dashed #000;
		.iconfont {
			font-size: 40rpx;
			padding: 0 20rpx;
		}
		input {
			flex: 1;
			font-size: 30rpx;
		}
	}
	.getCaptcha {
		box-shadow: 0 2rpx 2rpx rgba(100, 100, 100, 0.1);
		background: linear-gradient(to right, #3388ee, #4db9f1);
		color: #f8f8f8;
		margin: 40rpx 0;
	}
	.alternative {
		font-size: 30rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-around;
	}
}
</style>
